﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace Local
{
    public enum BugStatus
    {
        New = 0,
        Issue = 1,
        Requirement = 5,
        Update = 10,
        Open = 15,
        Fixing = 20,
        QA = 25,
        InLive = 30,
        WishList = 35,
        NotFeature = 40,
        Close = 100,
        Deleted = 1000
    }
    public static class CacheExtn
    {
        public static List<string> AllKeys = new List<string>();
        private static string keyPfx = "_UserApp_";

        public static bool HasFile(this HttpPostedFileBase file)
        {
            return (file != null && file.ContentLength > 0) ? true : false;
        }
        public static string KeyPrefix
        {
            get
            {

                return HttpContext.Current.Session.SessionID + CacheExtn.keyPfx;
            }
            set { CacheExtn.keyPfx = value; }
        }

        public static bool IsAppKey(string key)
        {
            if (key.StartsWith(KeyPrefix))
            {
                return true;
            }
            return false;
        }

        public static string GetKey(string key)
        {
            key = KeyPrefix + key;
            AllKeys.Add(key);
            return key;
        }
        public static string GetKey(this Controller page, string key)
        {
            key = KeyPrefix + key;
            AllKeys.Add(key);
            return key;
        }
        public static string GetAtoken()
        {
            return DateTime.Now.Ticks.ToString();
        }
        public static void Redirect(HttpContextBase context)
        {
            string url = context.Request.Url.AbsoluteUri;
            string token = GetAtoken();
            context.Response.Redirect(string.Format("/Home/Test/{0}?returnUrl={1}", token, url), true);
        }
        public static object GetCachedObject(this Controller page, string key)
        {
            return page.HttpContext.Cache[key];
        }
        public static void SetCachedObject(this Controller page, string key, object value)
        {
            page.HttpContext.Cache[key] = value;
        }

        public static void Clear(this Controller page)
        {
            foreach (DictionaryEntry entry in page.HttpContext.Cache)
            {
                string key = (string)entry.Key;
                if (CacheExtn.IsAppKey(key))
                {
                    page.HttpContext.Cache.Remove(key);
                }
            }
        }
        public static string GetVisitorIpAddress(this Controller page)
        {
            string stringIpAddress;
            stringIpAddress = page.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (stringIpAddress == null) //may be the HTTP_X_FORWARDED_FOR is null
                stringIpAddress = page.Request.ServerVariables["REMOTE_ADDR"]; //we can use REMOTE_ADDR
            else if (stringIpAddress == null)
                stringIpAddress = GetLanIPAddress();

            return stringIpAddress;
        }

        //Get Lan Connected IP address method
        public static string GetLanIPAddress()
        {
            //Get the Host Name
            string stringHostName = Dns.GetHostName();
            //Get The Ip Host Entry
            IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
            //Get The Ip Address From The Ip Host Entry Address List
            System.Net.IPAddress[] arrIpAddress = ipHostEntries.AddressList;
            return arrIpAddress[arrIpAddress.Length - 1].ToString();
        }

        public static int? ToNInt(this string intVal)
        {
            int good = 0;
            int.TryParse(intVal, out good);
            return new Nullable<int>(good);
        }
        public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
           where TEnum : struct, IComparable, IFormattable, IConvertible
        {
            string val = (Convert.ToInt32(enumObj)).ToString();
            var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                         select new { Id = (Convert.ToInt32(e)).ToString(), Name = e.ToString() };
            // var selectedValue = values.Where(x => x.Id == val).FirstOrDefault();
            return new SelectList(values, "Id", "Name", enumObj.ToString());
        }
        public static IEnumerable<SelectListItem> ToDropDownList(this Enum enumValue)
        {
            return from Enum e in Enum.GetValues(enumValue.GetType())
                   select new SelectListItem
                   {
                       Selected = e.Equals(enumValue),
                       Text = e.ToDescription(),
                       Value = e.ToInteger().ToString()
                   };
        }
        public static int ToInteger(this Enum enumValue)
        {
            int boss = Convert.ToInt32(enumValue);
            return boss;
        }
        public static string ToDescription(this Enum value)
        {
            var attributes = (DescriptionAttribute[])value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }
    }
}
